package com.demo2do.fms.admin.web.controller.supplier.appraisal;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.search.supplier.AppraisalSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.supplier.AppraisalResultService;
import com.demo2do.fms.admin.service.supplier.AppraisalService;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.entity.appraisal.Appraisal;
import com.demo2do.fms.entity.appraisal.AppraisalResultItem;
import com.demo2do.fms.entity.appraisal.AppraisalResult;

/**
 * 绩效考核评分
 * 
 * @author Wy
 *
 */
@Controller
@RequestMapping("/appraisal-scorings")
@MenuSetting("menu-supplier")
public class AppraisalScoringController {
    
    private static final Logger logger = LoggerFactory.getLogger(AppraisalScoringController.class);
    
    @Autowired
    private AppraisalService appraisalSerive;
    
    @Autowired
    private AppraisalResultService appraisalResultSerive;
    
    /**
     * list
     * 
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET )
    @MenuSetting("submenu-performance-appraisal-rate")
    public ModelAndView list(@Secure Login login, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/appraisal/appraisal-scoring/appraisal-scoring-list");
        
        int totals = appraisalSerive.count(login);
        if (totals > 0) {

            List<Appraisal> appraisals = appraisalSerive.list(login, page.with(totals));

            modelAndView.addObject("appraisals", appraisals);
            modelAndView.addObject("page", page);
            
        }

        return modelAndView;
        
    }

    /**
     * search panel
     *
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public ModelAndView search() {
        ModelAndView modelAndView = new ModelAndView("supplier/appraisal/appraisal-scoring/appraisal-scoring-search");
        return modelAndView;
    }

    /**
     * search
     * 
     * @param request
     * @param appraisalSearch
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-performance-appraisal-rate")
    public ModelAndView onSearch(AppraisalSearch search, @Secure Login login, 
                                 HttpServletRequest request, Page page) {

        ModelAndView modelAndView = new ModelAndView("supplier/appraisal/appraisal-scoring/appraisal-scoring-list");
        
        int totals = appraisalSerive.count(search, login);
        if (totals > 0) {
            
            List<Appraisal> appraisals = appraisalSerive.list(search, login, page.with(totals));
            modelAndView.addObject("appraisals", appraisals);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
            
        }

        return modelAndView;
        
    }

    /**
     * view
     * 
     * @param appraisalId
     * @return
     */
    @RequestMapping(value = "/{appraisalId}", method = RequestMethod.GET)
    @MenuSetting("submenu-performance-appraisal-rate")
    public ModelAndView view(@PathVariable("appraisalId") Long appraisalId) {

        ModelAndView modelAndView = new ModelAndView("supplier/appraisal/appraisal-scoring/appraisal-scoring-view");

        Appraisal appraisal = appraisalSerive.load(Appraisal.class, appraisalId);
        modelAndView.addObject("appraisal", appraisal);
        
        List<AppraisalResult> appraisalResults = appraisalResultSerive.list(appraisal);
        modelAndView.addObject("appraisalResults", appraisalResults);
        
        List<AppraisalResultItem> appraisalResultItems = appraisalResultSerive.listItem(appraisal);
        Map<String, List<AppraisalResultItem>> appraisalResultItemMaps = CollectionUtils.constructMapValues(appraisalResultItems, "result.id");
        modelAndView.addObject("appraisalResultItemMaps", appraisalResultItemMaps);

        return modelAndView;
        
    }
    
    /**
     * rate panel
     * 
     * @param resultId
     * @param appraisalId
     * @return
     */
    @RequestMapping(value = "/{appraisalId}/results/{resultId}/rate", method = RequestMethod.GET)
    @MenuSetting("submenu-performance-appraisal-rate")
    public ModelAndView rate(@PathVariable("appraisalId") Long appraisalId, @PathVariable("resultId") Long resultId) {

        ModelAndView modelAndView = new ModelAndView("supplier/appraisal/appraisal-scoring/appraisal-scoring-rate");
        
        AppraisalResult appraisalResult = appraisalResultSerive.load(AppraisalResult.class, resultId);
        List<AppraisalResultItem> AppraisalResultItems = appraisalResultSerive.listItem(appraisalResult);
        modelAndView.addObject("AppraisalResultItems", AppraisalResultItems);
        
        modelAndView.addObject("appraisalId", appraisalId);
        modelAndView.addObject("resultId", resultId);

        return modelAndView;
        
    }
    
    /**
     * rate
     * 
     * @param resultId
     * @param itemResults
     * @param operator
     * @return
     */
    @RequestMapping(value = "/{appraisalId}/results/{resultId}/rate", method = RequestMethod.PATCH , produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onRate(@PathVariable("resultId") Long resultId,
                                           @RequestParam("itemResults") List<String> itemResults,
                                           @Secure Login operator) {

        HttpResult httpResult = new HttpResult();

        try {

            AppraisalResult appraisalResult = appraisalResultSerive.load(AppraisalResult.class, resultId);
            appraisalResultSerive.rate(itemResults, appraisalResult, operator);

            httpResult.success().message("评分成功");

        } catch (BusinessException e) {
            logger.error("#onRate() - rate appraisal occur custom exception [{}]", e);
            httpResult.fail("500").message(e.getMessage());
        } catch (Exception e) {
            logger.error("#onRate() - rate appraisal occur system exception [{}]", e);
            httpResult.fail("500").message("评分失败");
        }

        return httpResult;
        
    }
    
}
